const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    Mindex:0,
    Mstart:0,
    ProductInStoreId: 0,
    ProductInStoreNo: '',
    proList: [],
    ProviderId: 0,
    ProviderName: "",
    HouseId: 0,
    HouseName: '',
    Remark: '',
    cteV: true,
    showTopTips: false,
    errorText:''
  },
  changeText: function (e) {
    let name = e.currentTarget.dataset.name;
    let nameMap = {}
    nameMap[name] = e.detail && e.detail.value
    this.setData(nameMap)
  },
  changeNum: function (e) {
    let name = e.currentTarget.dataset.name;
    let i = e.currentTarget.dataset.index;
    let arr = this.data.proList
    arr[i][name] = e.detail && e.detail.value
    this.setData({
      proList:arr
      })
  },
  //左移删除
  touchstart: function (e) {
    this.setData({
      index: e.currentTarget.dataset.index,
      Mstart: e.changedTouches[0].pageX
    });
  },
  touchmove: function (e) {
    //列表项数组
    let list = this.data.proList;
    //手指在屏幕上移动的距离
    //移动距离 = 触摸的位置 - 移动后的触摸位置
    let move = this.data.Mstart - e.changedTouches[0].pageX;
    // 这里就使用到我们之前记录的索引index
    //比如你滑动第一个列表项index就是0，第二个列表项就是1，···
    //通过index我们就可以很准确的获得当前触发的元素，当然我们需要在事前为数组list的每一项元素添加x属性
    list[this.data.index].x = move > 0 ? -move : 0;
    this.setData({
      proList: list
    });
  },
  touchend: function (e) {
    let list = this.data.proList;
    let move = this.data.Mstart - e.changedTouches[0].pageX;
    list[this.data.index].x = move > 100 ? -180 : 0;
    this.setData({
      proList: list
    });
  },
  delPro: function(e){
    var i=e.currentTarget.dataset.index
    var arr=this.data.proList
    arr.splice(i,1)
    this.setData({
      proList:arr
    })
  },
  compReq: function () {
    var that = this
    var stop = true
    var arr = this.data.proList
    if (arr.length > 0) {
      for (let i = 0; i < arr.length; i++) {
        delete arr[i].maxNum
        delete arr[i].x
        arr[i].Quantity *= 1
        console.log(arr[i].Quantity)
        if (arr[i].Quantity && arr[i].Quantity != '' && arr[i].Quantity != 0) {
        } else {
          this.setData({
            showTopTips: true,
            errorText: '请输入数量'
          })
          setTimeout(function () {
            that.setData({
              showTopTips: false,
              errorText: ''
            })
          }, 2000)
          stop = false
          return false
        }
      }
    } else {
      this.setData({
        showTopTips: true,
        errorText: '至少选择一件产品'
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false,
          errorText: ''
        })
      }, 2000)
      stop = false
      return false
    }
    if (stop) {
      wx.request({
        url: app.globalData.api + 'HouseStore/ProductInStore',
        method: "POST",
        header: {
          Authorization: app.globalData.Authorization
        },
        data: {
          ProductInStoreId: this.data.ProductInStoreId,
          // ProviderId: this.data.ProviderId,
          // ProviderName: this.data.ProviderName,
          // HouseId: this.data.HouseId,
          // HouseName: this.data.HouseName,
          Remark: this.data.Remark,
          productInStoreDetails: this.data.proList
        },
        success: function (res) {
          console.log(res)
          if (res.data.Code) {
            wx.navigateBack({
              delta: 1
            })
          } else {
            that.setData({
              showTopTips: true,
              errorText: res.data.Error
            })
            setTimeout(function () {
              that.setData({
                showTopTips: false
              }, 2000)
            })
          }
        }
      })

    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var that=this
    if(options.id){
      this.setData({
        cteV: false
      })
      app.req({
        url: 'HouseStore/ProductInStore/' + options.id,
        success: function (res) {
          console.log(res.data)
          var data = res.data.Result
          that.setData({
            ProductInStoreId: data.ProductInStoreId,
            ProductInStoreNo: data.ProductInStoreNo,
            // ProviderId: data.ProviderId,
            // ProviderName: data.ProviderName,
            // HouseId: data.HouseId,
            // HouseName: data.HouseName,
            Remark: data.Remark,
            proList: data.productInStoreDetails
          })
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})